<?php
namespace Admin\Controller;
use Think\Controller;
use Org\Util\String;
use Think\Page;
use Think\Upload;

	class CommonController extends Controller{
		
		var $flag = array(
				'normal'=>'新闻',
				'viewspot'=>'景点',
				'meeting'=>'会务',
				'restaurant'=>'餐厅',
				'roomtype'=>'房型',
			);
		
		Public function _initialize(){
   		// 初始化的时候检查用户权限
   			if(!isset($_SESSION['is_login']) || $_SESSION['is_login']=='' || $_SESSION['is_login']==0){
				$this->redirect('Login/login');
			}else {
				$this->assign('my_flag',$this->flag);
			}
		}
		
		
		/**
		 * 
		 * 传入true时  说明是 有管理查询的 
		 */
		public function index($is_rela=false){
			$this -> getwithsearch($is_rela);
		}
		
		//这个 是 公共的 可以在外面 调用
		public  function getwithoutsearch(){ // 不带 搜索条件的 获取 基本不带管理操作 
			
			$my_M = M(CONTROLLER_NAME); 
			
			$count      = $my_M->count();
			$Page       = new Page($count,6);
			$show       = $Page->show();
			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			$list = $my_M-> where($map)->limit($Page->firstRow.','.$Page->listRows)->order('created_at desc')->select();
			$this->assign('data',$list);
			$this->assign('page',$show);
			
			$this->display(CONTROLLER_NAME.'/index');
		}
		
		//这个是 必须在内部 调用 
		private  function getwithsearch($is_rela){ 
			
			if($is_rela){
				$my_M = D(CONTROLLER_NAME); 
			}else {
				$my_M = M(CONTROLLER_NAME); 
			}
			$map = array(); 
    		$parameter = array();
			
    		$title = $_GET['title'];
			$username = $_GET['username'];
			$category_id = $_GET['category_id'];
			$flag = $_GET['flag'];
			
			if (!$title == NULL){
				$map['title'] = array('like','%'.$title.'%'); 
				$parameter=array(
					'title'=>$title,
				);
				$this->assign('title',$title);
			}
			if (!$username == NULL){
				$map['username'] = array('like','%'.$username.'%'); 
				$parameter=array(
					'username'=>$username,
				);
				$this->assign('username',$username);
			}
			if (!$category_id == NULL){
				$map['category_id'] = array('eq',$category_id); 
				$parameter['category_id'] = $category_id;
				$this->assign('category_id',$category_id);
			}
			if (!$flag == NULL){
				$map['flag'] = array('eq',$flag); 
				$parameter['flag'] = $flag;
				$this->assign('flag',$flag);
				$this->assign('flagname',$this->flag[$flag]);
			}
			
			$count      = $my_M-> where($map)->count();// 查询满足要求的总记录数
			$Page       = new Page($count,6,$parameter);// 实例化分页类 传入总记录数和每页显示的记录数
			$show       = $Page->show();// 分页显示输出
			// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
			if($is_rela){
				$my_M-> relation(true);
			}
			
			$list = $my_M-> where($map)->limit($Page->firstRow.','.$Page->listRows)->order('created_at desc')->select();
			$this->assign('data',$list);
			$this->assign('page',$show);
			$this->display(CONTROLLER_NAME.'/index');
		}
		
		
		public function del(){
			$m=M(CONTROLLER_NAME);
			$id=$_GET['id'];
			$count=$m->delete($id);
			if($count>0){
				$this->show('del');
			}else{
				$this->show('error');
			}
		}
		
		public function lock(){
			$m=M(CONTROLLER_NAME);
			$id=$_GET['id'];
			$sta = $m-> where('id='.$id)->getField('status');
			if($sta>0){
				$m ->where('id='.$id)->setField('status','0');
				$this->show('unlock');
			}else{
				$m ->where('id='.$id)->setField('status','1');
				$this->show('lock');
			}
		}
		
		
		public function modify(){
			
			$id=$_GET['id'];
			$m=M(CONTROLLER_NAME);
			$arr=$m->find($id);

			$arr['title']  = str_replace('<br/>','\r\n',$arr['title']);

			$this->assign('data',$arr);
			$flag = $arr['flag'];
			if($flag){
				$this->assign('flagname',$this->flag[$flag]);
				$this->assign('flagurl','/flag/'.$flag);
			}


			
			$this->display(CONTROLLER_NAME.'/edit');
		}
		
		public function update($is_file=false){
			$m=D(CONTROLLER_NAME);
			if($is_file){
				$upload = new \Think\Upload();   
				$upload->maxSize   =     3145728 ;
				$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');
				$upload->savePath  =      '/images/'; 
				// 上传文件 
				$info   =   $upload->uploadOne($_FILES['fileselect']);
				
				
				if(!$info) {// 上传错误提示错误信息
					unset($_POST['img']);
				}else{// 上传成功 获取上传文件信息
				    $_POST['img'] = '/Uploads'.$info['savepath'].$info['savename'];
					
					$imginfo = $m-> where('id='.$_POST['id'])->getField('img');
					//删除 原来的 图片 
					if(is_file(__ROOT__.$imginfo)) {
						//echo "删除 员图片 ";
						unlink(__ROOT__.$imginfo);
					}
				}
			}

			//转换回车
			$_POST['title'] = nl2br($_POST['title']);

			
			if(!$m->create()){
				$this->error($m->getError());
			}
			
			$idNum=$m->save();
			if($idNum>0){
				$this->success('修改成功','index');
			}else{
				$this->error('数据修改失败');
			}
		}
		
		
		public function add(){
			
			$this->display(CONTROLLER_NAME.'/add');
		}
		
		public function create($is_file=false){
			
			if($is_file){
				$this -> insertwithfile();
			}else $this -> insertwithoutfile();
			
		}
		
		public function insertwithoutfile(){
			$m=D(CONTROLLER_NAME);
			if(!$m->create()){
				$this->error($m->getError());
			}
			
			$idNum=$m->add();
			if($idNum>0){
				$this->success('添加数据成功','index');
			}else{
				var_dump($m);
				//$this->error('数据添加失败');
			}
		}
		
		private function insertwithfile(){
			
			$upload = new \Think\Upload();   
			$upload->maxSize   =     3145728 ;
			$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');
			$upload->savePath  =      '/images/'; 
			// 上传文件 
			$info   =   $upload->uploadOne($_FILES['fileselect']);
			if(!$info) {// 上传错误提示错误信息
			    $this->error($upload->getError());
			}else{// 上传成功 获取上传文件信息
			    $_POST['img'] = '/Uploads'.$info['savepath'].$info['savename'];
				$m=D(CONTROLLER_NAME);
				if(!$m->create()){
					$this->error($m->getError());
				}
				
				$idNum=$m->add();
				if($idNum>0){
					$this->success('添加数据成功','index');
				}else{
					$this->error('数据添加失败');
				}
			}
			
		}
		
		
	}
?>
